package com.liu.common.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.liu.qqRobot.wm.entity.dto.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class WmClientUtils {

    /**
     * 获取warframe.market的物品
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketItemsDTO> getItem() {
        String itemsUrl = "https://api.warframe.market/v1/items";
        String response = HttpRequest.get(itemsUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/items】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("items").toList(WarframeMarketItemsDTO.class);
    }

    /**
     * 获取warframe.market的物品详情
     * @return List<WarframeMarketItemsDTO>
     */
    public static WarframeMarketItemsDetailDTO getItemDetail(String urlName) {
        String itemsUrl = "https://api.warframe.market/v1/items/" + urlName;
        String response = HttpRequest.get(itemsUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/items】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONObject("item").toBean(WarframeMarketItemsDetailDTO.class);
    }

    /**
     * 获取warframe.market的赤毒物品
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketItemsDTO> getLichWeapons() {
        String lichItemsUrl = "https://api.warframe.market/v1/lich/weapons";
        String response = HttpRequest.get(lichItemsUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/lich/weapons】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("weapons").toList(WarframeMarketItemsDTO.class);
    }

    /**
     * 获取warframe.market的赤毒物品怪癖
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketItemsDTO> getLichQuirks() {
        String lichItemsUrl = "https://api.warframe.market/v1/lich/quirks";
        String response = HttpRequest.get(lichItemsUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/lich/quirks】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("quirks").toList(WarframeMarketItemsDTO.class);
    }

    /**
     * 获取warframe.market的赤毒物品幻纹
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketItemsDTO> getLichEphemeras() {
        String lichItemsUrl = "https://api.warframe.market/v1/lich/ephemeras";
        String response = HttpRequest.get(lichItemsUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/lich/ephemeras】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("ephemeras").toList(WarframeMarketItemsDTO.class);
    }

    /**
     * 获取warframe.market的信条物品
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketItemsDTO> getSisterWeapons() {
        String lichItemsUrl = "https://api.warframe.market/v1/sister/weapons";
        String response = HttpRequest.get(lichItemsUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/sister/weapons】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("weapons").toList(WarframeMarketItemsDTO.class);
    }

    /**
     * 获取warframe.market的信条物品怪癖
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketItemsDTO> getSisterQuirks() {
        String lichItemsUrl = "https://api.warframe.market/v1/sister/quirks";
        String response = HttpRequest.get(lichItemsUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/sister/quirks】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("quirks").toList(WarframeMarketItemsDTO.class);
    }

    /**
     * 获取warframe.market的信条物品幻纹
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketItemsDTO> getSisterEphemeras() {
        String lichItemsUrl = "https://api.warframe.market/v1/sister/ephemeras";
        String response = HttpRequest.get(lichItemsUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/sister/ephemeras】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("ephemeras").toList(WarframeMarketItemsDTO.class);
    }

    /**
     * 获取warframe.market的紫卡
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketItemsDTO> getRivenItems() {
        String lichItemsUrl = "https://api.warframe.market/v1/riven/items";
        String response = HttpRequest.get(lichItemsUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/riven/items】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("items").toList(WarframeMarketItemsDTO.class);
    }

    /**
     * 获取warframe.market的紫卡属性
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketItemsDTO> getRivenAttributes() {
        String lichItemsUrl = "https://api.warframe.market/v1/riven/attributes";
        String response = HttpRequest.get(lichItemsUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/riven/attributes】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("attributes").toList(WarframeMarketItemsDTO.class);
    }

    /**
     * 获取warframe.market的订单
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketItemsOrderDTO> getItemOrders(String urlName) {
        String lichItemOrdersUrl = "https://api.warframe.market/v1/items/" + urlName + "/orders";
        String response = HttpRequest.get(lichItemOrdersUrl)
                .header("Language", "zh-hans")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/lich/weapons】返回：{}", response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("orders").toList(WarframeMarketItemsOrderDTO.class);
    }

    /**
     * 获取warframe.market的玄骸拍卖
     * @return List<WarframeMarketItemsDTO>
     */
    public static List<WarframeMarketLichAuctionDTO> getLichAuctions(String type, String buyoutPolicy, String urlName, String element,
                                                                     Boolean havingEphemera, Integer damageMin, Integer damageMax,
                                                                     String quirkUrlName, String sortBy) {
        StringBuilder lichItemAuctionsUrl = new StringBuilder("https://api.warframe.market/v1/auctions/search");
        lichItemAuctionsUrl.append("?type=").append(type);
        lichItemAuctionsUrl.append("&weapon_url_name=").append(urlName);
        if (StrUtil.isNotEmpty(buyoutPolicy)) {
            lichItemAuctionsUrl.append("&buyout_policy=").append(buyoutPolicy);
        }
        if (StrUtil.isNotEmpty(element)) {
            lichItemAuctionsUrl.append("&element=").append(element);
        }
        if (havingEphemera != null) {
            lichItemAuctionsUrl.append("&having_ephemera=").append(havingEphemera);
        }
        if (damageMin != null) {
            lichItemAuctionsUrl.append("&damage_min=").append(damageMin);
        }
        if (damageMax != null) {
            lichItemAuctionsUrl.append("&damage_max=").append(damageMax);
        }
        if (StrUtil.isNotEmpty(quirkUrlName)) {
            lichItemAuctionsUrl.append("&quirk=").append(quirkUrlName);
        }
        if (StrUtil.isNotEmpty(sortBy)) {
            lichItemAuctionsUrl.append("&sort_by=").append(sortBy);
        }
        String response = HttpRequest.get(lichItemAuctionsUrl.toString())
                .header("Platform", "pc")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/auctions/search?type={}】返回：{}", type, response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("auctions").toList(WarframeMarketLichAuctionDTO.class);
    }

    /**
     * 获取warframe.market的紫卡拍卖
     * @param type 拍卖类型
     * @param buyoutPolicy 拍卖类型 auction（拍卖）, direct（售卖）
     * @param urlName 紫卡
     * @param positiveStats 正面属性 3个
     * @param negativeStats 负面属性
     * @param operation anyOf, allOf
     * @param masteryRankMin 最小段位
     * @param masteryRankMax 最大段位
     * @param reRollsMin 最小循环次数
     * @param reRollsMax 最大循环次数
     * @param modRank 紫卡等级 any, maxed
     * @param polarity madurai, vazarin, naramon, any
     * @param sortBy 排序 price_desc, price_asc, positive_attr_desc, positive_attr_asc
     * @return List<WarframeMarketRivenAuctionDTO>
     */
    public static List<WarframeMarketRivenAuctionDTO> getRivenAuctions(String type, String buyoutPolicy, String urlName, String positiveStats,
                                                                       String negativeStats, String operation, Integer masteryRankMin,
                                                                       Integer masteryRankMax, Integer reRollsMin, Integer reRollsMax, String modRank,
                                                                       String polarity, String sortBy) {
        StringBuilder rivenAuctionsUrl = new StringBuilder("https://api.warframe.market/v1/auctions/search");
        rivenAuctionsUrl.append("?type=").append(type);
        if (StrUtil.isNotEmpty(urlName)) {
            rivenAuctionsUrl.append("&weapon_url_name=").append(urlName);
        }
        if (StrUtil.isNotEmpty(buyoutPolicy)) {
            rivenAuctionsUrl.append("&buyout_policy=").append(buyoutPolicy);
        }
        if (StrUtil.isNotEmpty(positiveStats)) {
            rivenAuctionsUrl.append("&positive_stats=").append(positiveStats);
        }
        if (StrUtil.isNotEmpty(negativeStats)) {
            rivenAuctionsUrl.append("&negative_stats=").append(negativeStats);
        }
        if (StrUtil.isNotEmpty(operation)) {
            rivenAuctionsUrl.append("&operation=").append(operation);
        }
        if (masteryRankMin != null) {
            rivenAuctionsUrl.append("&mastery_rank_min=").append(masteryRankMin);
        }
        if (masteryRankMax != null) {
            rivenAuctionsUrl.append("&mastery_rank_max=").append(masteryRankMax);
        }
        if (reRollsMin != null) {
            rivenAuctionsUrl.append("&re_rolls_min=").append(reRollsMin);
        }
        if (reRollsMax != null) {
            rivenAuctionsUrl.append("&re_rolls_max=").append(reRollsMax);
        }
        if (StrUtil.isNotEmpty(modRank)) {
            rivenAuctionsUrl.append("&mod_rank=").append(modRank);
        }
        if (StrUtil.isNotEmpty(polarity)) {
            rivenAuctionsUrl.append("&polarity=").append(polarity);
        }
        if (StrUtil.isNotEmpty(sortBy)) {
            rivenAuctionsUrl.append("&sort_by=").append(sortBy);
        }
        String response = HttpRequest.get(rivenAuctionsUrl.toString())
                .header("Platform", "pc")
                .timeout(60000)
                .execute()
                .body();
//        log.info("请求Warframe.market接口【/auctions/search?type={}】返回：{}", type, response);
        JSONObject jsonObject = JSONUtil.parseObj(response);
        JSONObject payload = jsonObject.getJSONObject("payload");
        return payload.getJSONArray("auctions").toList(WarframeMarketRivenAuctionDTO.class);
    }
}
